import os
import imageio
import collections
import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# mask = imageio.imread("D:\桌面\考研大数据\数据分析\背景.jpg") #词云背景图片

folder_path = 'D:\桌面\考研大数据\数据分析\dataWash'  # 需要读取的文件夹
csv_files = [os.path.join(folder_path, f) for f in os.listdir(
    folder_path) if f.endswith('.csv')]  # 提取文件夹里的所有csv文件
word_list = []  # 存词

for file in csv_files:
    data = pd.read_csv(file, header=0)  # 开始读取csv文件
    for obj in data['考试科目']:  # 存考试专业科目
        word_list.append(obj.split(')')[-1])
    for college in data['院系所']:  # 存院系
        word_list.append(college.split(')')[-1])
    for zhuanye in data['专业']:  # 存专业
        word_list.append(zhuanye.split(')')[-1])
    for research in data['研究方向']:  # 存研究方向
        word_list.append(research.split(')')[-1])

# with open('D:\桌面\考研大数据\数据分析\worldCloud.txt', 'a', newline='',encoding="utf-8") as f:
#     csv_write = csv.writer(f)
#     for worlds in word_list:
#         f.write(worlds+',')

word_counts = collections.Counter(word_list)

# 绘制词云
my_cloud = WordCloud(
    background_color='white',  # 设置背景颜色  默认是black
    width=900, height=500,
    font_path='simhei.ttf',    # 设置字体  显示中文
    max_font_size=100,         # 设置字体最大值
    min_font_size=15,          # 设置子图最小值
    random_state=60,            # 设置随机生成状态，即多少种配色方案
    # mask=mask
).generate_from_frequencies(word_counts)

# 显示生成的词云图片
plt.imshow(my_cloud, interpolation='bilinear')
# 显示设置词云图中无坐标轴
plt.axis('off')
plt.show()
